package com.shunda.ysdgj.manager.service.impl.goods;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.shunda.ysdgj.common.exception.CustomException;
import com.shunda.ysdgj.common.util.ValidateUtil;
import com.shunda.ysdgj.manager.mapper.goods.GoodsCategoryMapper;
import com.shunda.ysdgj.manager.service.goods.GoodsCategoryService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.entity.goods.GoodsCategory;
import com.shunda.ysdgj.model.vo.ResultCodeEnum;
import com.shunda.ysdgj.model.vo.goods.GoodsCategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsCategoryServiceImpl
        extends MPJBaseServiceImpl<GoodsCategoryMapper, GoodsCategory>
        implements GoodsCategoryService, MPJBaseService<GoodsCategory> {

    public List<GoodsCategory> findByParentId(Long parentId) {
        if (ValidateUtil.isNotLong(parentId)) {
            parentId = 0L;
        }
        List<GoodsCategory> categoryList = lambdaQuery()
                .eq(GoodsCategory::getParentId, parentId)
                .orderByAsc(GoodsCategory::getCategorySort).list();
        for (GoodsCategory category : categoryList) {
            category.setHasChildren(lambdaQuery().eq(GoodsCategory::getParentId, category.getId()).count() > 0);
        }
        return categoryList;
    }

    public boolean add(GoodsCategory goodsCategory) {
        if (ValidateUtil.isBlank(goodsCategory.getCategoryName())
                || ValidateUtil.isNotInteger(goodsCategory.getCategorySort())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        Long count = lambdaQuery()
                .eq(GoodsCategory::getCategoryName, goodsCategory.getCategoryName())
                .count();
        if (count > 0) {
            throw new CustomException(ResultCodeEnum.GOODS_CATEGORY_REPEAT);
        }
        goodsCategory.setId(null);
        return save(goodsCategory);
    }

    public boolean modify(GoodsCategory goodsCategory) {
        if (ValidateUtil.isNotLong(goodsCategory.getId())
                || ValidateUtil.isBlank(goodsCategory.getCategoryName())
                || ValidateUtil.isNotInteger(goodsCategory.getCategorySort())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return updateById(goodsCategory);
    }

    @Transactional
    public boolean deleteByIds(IdsDto idsDto) {
        List<Long> ids = idsDto.getIds();
        if (ids.isEmpty()) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        Long count = lambdaQuery().in(GoodsCategory::getParentId, ids).count();
        if (count > 0) {
            throw new CustomException(ResultCodeEnum.GOODS_CATEGORY_ERROR);
        }
        return removeByIds(ids);
    }

}
